package com.template.app.editor;

import java.util.Date;
import java.util.Set;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Component;

import com.template.app.model.TemplateMany;
import com.template.app.model.TemplateRef;
import com.template.app.service.GenericService;

@Component
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
	
	@Autowired
	private GenericService service;

	public void registerCustomEditors(PropertyEditorRegistry registry) {
		registry.registerCustomEditor(String.class, new StringTrimmerEditor(false));
		registry.registerCustomEditor(Date.class, new DateEditor());
		
		registerObjectEditor(TemplateRef.class, registry);
		
		registerSetEditor(TemplateMany.class, "templateManys", registry);
	}
	
	private void registerObjectEditor(Class<?> clazz, PropertyEditorRegistry registry) {
        registry.registerCustomEditor(clazz, new ObjectEditor(clazz, service));    	
    }
	
	private void registerSetEditor(Class<?> clazz, String path, PropertyEditorRegistry registry) {
        registry.registerCustomEditor(Set.class, path, new SetEditor(clazz, service));
    }

}
